package com.wctj.api.base.async;

import com.wctj.api.base.dto.JwtUserDTO;
import com.wctj.api.base.utils.UserContext;

/**
 * @author qy
 * @since 2023/4/11 14:59
 */
public class UserContextRunnable implements Runnable {

    private final JwtUserDTO currentUser;

    private final Runnable runnable;

    public UserContextRunnable(JwtUserDTO currentUser, Runnable runnable) {
        this.currentUser = currentUser;
        this.runnable = runnable;
    }

    @Override
    public void run() {
        try {
            UserContext.setContextUserWithFeignHeader(currentUser);
            runnable.run();
        } finally {
            UserContext.removeContextUserWithFeignHeader();
        }
    }
}